package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase{

	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().contactsPage();
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomePage();
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		initContactModification(index);	
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		return this;
		
	}
	
	public ContactHelper deleteContact(int index){
		selectContactByIndex(index);
		submitContactDeletion();
		returnToHomePage();
		return this;
	}
	
	//----------------------------------------------------------------------------------
	
	public SortedListOf<ContactData> getUiContacts() {
		SortedListOf<ContactData> contacts = new SortedListOf<ContactData>();
		
		manager.navigateTo().contactsPage();
		List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			String firstname = title.substring("Select (".length(), title.length() - ")".length());
			contacts.add(new ContactData().withFirstname(firstname));
		}
		return contacts;
	}
	
	private void selectContactByIndex(int index) {		
		click(By.xpath("//input[@name='selected[]']["+ (index + 1) +"]"));
	}
	

	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress_one());
		type(By.name("home"), contact.getPhonehome());
		type(By.name("mobile"), contact.getPhonemobile());
		type(By.name("work"), contact.getWork());
		type(By.name("email"), contact.getMail_one());	
		type(By.name("email2"), contact.getMail_two());	
		selectByText(By.name("bday"), contact.birthDay);
		selectByText(By.name("bmonth"), contact.birthMonth);
		type(By.name("byear"), contact.birthYear);
		if (formType == CREATION) {
			//selectByText(By.name("new_group"), "group 1");
		} else{
			if(driver.findElements(By.name("new_group")).size() != 0){
				throw new Error ("Group selector exists in contact modification form");
			}
		}
		type(By.name("address2"), contact.getAddress_two());	
		type(By.name("phone2"), contact.getHome_two());
		return this;
	}


	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}
	
	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		return this;
	}

	public void submitContactDeletion() {		
		click(By.xpath("//input[@name='update' and @value='Delete']"));	
	}
	
	public ContactHelper initContactModification(int id) {
		click(By.xpath("//a[@href='edit.php?id=" + id + "']"));
		return this;
			}
	
	public ContactHelper submitContactModification() {
		click(By.xpath("//input[@name='update' and @value='Update']"));
		return this;
	}
	
}
